package com.atguigu.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.yygh.cmn.listener.DictListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.model.cmn.Dict;
import com.atguigu.yygh.common.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService
{

    /**
     * 根据上级id获取子节点数据列表
     * @return
     */
    @Override
    public List<Dict> findChildData(Long id) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Dict> dictList = baseMapper.selectList(wrapper);

        //向List集合每个对象中设置hasChildren
        for (Dict dict : dictList) {
            Long dictId = dict.getId();
            Boolean children = isChildren(dictId);
            dict.setHasChildren(children);
        }
        return dictList;
    }

    /**
     * 判断id下面是否有子节点
     */
    public Boolean isChildren(Long id){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Integer count = baseMapper.selectCount(wrapper);
        return count>0;
    }

    /**
     * 导出数据字典
     * @param response
     */
    @Override
    public void exportData(HttpServletResponse response) {
        //设置头信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "dict";
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        //将数据查出来
        List<Dict> dictList = baseMapper.selectList(null);
        List<DictEeVo> dictEeVoList = new ArrayList<>();

        //遍历dictList集合,得到每一个Dict对象
        for (Dict dict : dictList) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVoList.add(dictEeVo);
        }

        try {
            EasyExcel.write(response.getOutputStream(),DictEeVo.class)
                    .sheet("dict")
                    .doWrite(dictEeVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 导入数据字典
     *
     * @param file
     */
    @Override
    public void importData(MultipartFile file)
    {
        try
        {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper))
            .sheet()
            .doRead();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 查询数据字典名称
     *
     * @param dictCode 字典编码
     * @param value    字典值
     * @return 字典名称
     */
    @Override
    public String getName(String dictCode, String value) {
        if (StringUtils.isEmpty(dictCode)){
            QueryWrapper<Dict> wrapper = new QueryWrapper<>();
            wrapper.eq("value",value);
            Dict dict = baseMapper.selectOne(wrapper);
            return dict.getName();
        }else {
            Dict dict = getDictName(dictCode);
            Long dictId = dict.getId();
            Dict dictP = baseMapper.selectOne(new QueryWrapper<Dict>()
                    .eq("parent_id", dictId)
                    .eq("value", value));
           return dictP.getName();
        }
    }

    //工具类
    private Dict getDictName(String dictCode){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code",dictCode);
        Dict dict = baseMapper.selectOne(queryWrapper);
        return dict;
    }

    /**
     * 根据dictCode获取下级节点
     *
     * @param dictCode 省份编码
     * @return
     */
    @Override
    public List<Dict> findByDictCode(String dictCode) {
        //1. 根据dictCode获取到省分类
        Dict dict = this.getDictName(dictCode);
        //2. 拿到id，根据id查询子节点
        List<Dict> dictList = this.findChildData(dict.getId());
        return dictList;
    }
}
